﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CGSR.Core
{
    public class SimulationPacketsManager
    {
        //public ICollection<Packet>[] Packets { get; set; }
        public int MaxPacketSize { get; set; }
        public int MinPacketSize { get; set; }
        public int ReceptionRadius { get; set; }
        public int Power { get; set; }
        public int QueueSize { get; set; }
        
        protected int NumOfTicks { get; set; }
        protected int NumOfUsers { get; set; }

        StreamReader readFile;

        public SimulationPacketsManager(string trafficFilePath)
        {
           readFile = new StreamReader(trafficFilePath);
                
            string line;

            ///  first line
            if ((line = readFile.ReadLine()) != null)
            {
                ParseFirstRow(line);
            }
            
            
                
            
        }

        private void ParseFirstRow(string line)
        {
            string[] row = line.Split(',');

            int numofticks;
            if (int.TryParse(row[1].Split('=')[1], out numofticks))
            {
                this.NumOfTicks = numofticks;
            }
            else throw new Exception("No NumOfTicks in the Traffic file");

            int numofusers;
            if (Int32.TryParse(row[0].Split('=')[1], out numofusers))
            {
                this.NumOfUsers = numofusers;
            }
            else throw new Exception("No NumOfUsers in the Traffic file");

            int maxPktSize;
            if (Int32.TryParse(row[3].Split('=')[1], out maxPktSize))
            {
                this.MaxPacketSize = maxPktSize;
            }
            else throw new Exception("No MaxPacketSize in the Traffic file");

            int minPktSize;
            if (Int32.TryParse(row[2].Split('=')[1], out minPktSize))
            {
                this.MinPacketSize = minPktSize;
            }
            else throw new Exception("No MinPacketSize in the Traffic file");
                                
            int receptionRadius;
            if (Int32.TryParse(row[4].Split('=')[1], out receptionRadius))
            {
                this.ReceptionRadius = receptionRadius;
            }
            else throw new Exception("No MinPacketSize in the Traffic file");
            
            int power;
            if (Int32.TryParse(row[5].Split('=')[1], out power))
            {
                this.Power = power;
            }
            else throw new Exception("No MinPacketSize in the Traffic file");

            int queueSize;
            if (Int32.TryParse(row[6].Split('=')[1], out queueSize))
            {
                this.QueueSize = queueSize;
            }
            else throw new Exception("No MinPacketSize in the Traffic file");
        }

        public ICollection<Packet> GetTrafficForNextTick()
        {
            string[] row;
            var line = readFile.ReadLine();
            
            row = line.Split(',');
//            if (row.Length == 0 || row[0].Length == 0 || row[0][0] == '#')
//                continue;
            int tick = int.Parse(row[0].Replace("T", ""));
            List<Packet> packets = new List<Packet>();
            for (int i = 1; i + 3 < row.Length; i = i + 4)
            {
                Packet p = new Packet()
                {
                    Source = int.Parse(row[i]),
                    Destination = int.Parse(row[i + 1]),
                    ID = int.Parse(row[i + 2]),
                    Size = int.Parse(row[i + 3]),
                    SentOnTick = tick
                };
                packets.Add(p);
            }
            return packets;
            
        }
    }
}
